<?php

namespace App\Http\Controllers\platform;

use App\Http\Controllers\Controller;
use App\Models\DutiesAccept;
use App\Models\DutiesCourse;
use App\Models\StudyLog;
use Illuminate\Http\Request;

class TaskController extends Controller
{
    public function studyInfo(Request $Request)
    {
        $state = $Request->state;
        $limit = $Request->limit ?: 10;
        $page = $Request->page ?: 1;
        $page = $page * $limit - $limit;
        $task_id = $Request->task_id;
        $DutiesCourse = DutiesCourse::select('id','video_id','course_id')->where('task_id',$task_id)->get();
        $stusys = 0;
        foreach ($DutiesCourse as $k => $v) {
            $video = explode(',',$v->video_id);
            $video = count($video);
            $stusys += $video;
        }

        //先查询任务绑定的用户
        $sql = DutiesAccept::select('id','uid','is_complete')->where('task_id',$task_id)->with('user')
                ->withCount(['studys' => function ($query) use ($task_id) {
                    $query->where('task_id', $task_id);
                }])
                ->withCount(['papers' => function ($query) use ($task_id) {
                    $query->where('task_id', $task_id);
                }])
                ->withCount(['answers' => function ($query) use ($task_id) {
                    $query->where('tid', $task_id);
                }]);
        if($state == 1){//已完成
            
        }

        $total = $sql->count();
        $data = $sql->get();
        // $data = $sql->skip($page)->take($limit)->get();
        foreach ($data as $k => $v) {
            $status = '未开始';
            // if($v->is_complete == 1){
            //     $status = '已完成';
            // }
            
            // if($v->is_complete == 0 && $v->studys_count){
            //     $status = '进行中';
            // }
            if($v->studys_count > 0 && $v->studys_count < $stusys){
                $status = '进行中';
            }
            if($v->studys_count >= $stusys){
                $status = '已完成';
                $v->studys_count = $stusys;
            }
            $data[$k]->status = $status;
            $data[$k]->totals = $stusys;
        }
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data,'total'=>$total]);
    }

}
